<template>
    <div id="timeline" v-loading="loading">
        <div class="scrubber" :style="{ left: scrubberLeft }"></div>
        <div
            class="tick"
            v-for="(item, index) of ticks"
            :key="index"
            :style="{ left: item.left }"
        >
            {{ loading ? "" : item.tickTime }}
        </div>
    </div>
</template>
<script>
// import { mapState } from "vuex";
import { formatTime, interval } from "./../../utils/string.js";
export default {
    data() {
        return {
            timeline: null,
            video: null,
            ticks: [],
            loading: false,
            scrubberLeft: "0",
        };
    },
    props: ["obj", "duration"],
    watch: {
        duration(newDuration) {
            if (!newDuration) return;
            this.loading = true;
            setTimeout(() => {
                let hasHours = false;
                if (Math.floor(newDuration / 3600) > 0) {
                    hasHours = true;
                }
                const tmp = interval(newDuration);
                this.ticks = tmp.map((item, index) => {
                    const y = (index / (tmp.length - 1)) * 100;
                    return {
                        left: `${y.toFixed(2)}%`,
                        tickTime: formatTime(item, hasHours),
                    };
                });
                this.loading = false;
            });
        },
    },
    // computed: {
    //     ...mapState({
    //         videoList: (state) => state.app.videoList,
    //         videoSelected: (state) => state.app.videoSelected,
    //     }),
    // },
    mounted() {
        this.$nextTick(() => {
            this.timeline = document.getElementById("timeline");
            this.video = document.getElementById("video");
            this.video.addEventListener("timeupdate", this.updateScrubber);
            this.timeline.addEventListener("click", this.updateVideoTime);
        });
    },
    methods: {
        // 根据视频时间更新滑块位置
        updateScrubber() {
            requestAnimationFrame(() => {
                const time = this.video.currentTime;
                if (this.obj.videoList[this.obj.videoSelected]) {
                    this.$set(
                        this.obj.videoList[this.obj.videoSelected],
                        "currentSecond",
                        Math.floor(this.video.currentTime)
                    );
                }
                const duration = this.video.duration;
                let position = (time / duration) * 100;
                if (time === 0) {
                    position = 0;
                }
                this.scrubberLeft = `${position.toFixed(2)}%`;
            });
        },
        // 鼠标点击底部区域更新视频时间
        updateVideoTime(event) {
            if (this.video) {
                var targetRect = this.timeline.getBoundingClientRect();
                var targetX = targetRect.left;
                const position = event.clientX - targetX - 2;

                const duration = this.video.duration;
                const time = (position / this.timeline.offsetWidth) * duration;
                this.video.currentTime = time;
                if (this.obj.videoList[this.obj.videoSelected]) {
                    this.obj.videoList[this.obj.videoSelected].currentSecond =
                        Math.floor(this.video.currentTime);
                }
                this.video.pause();
            }
        },
    },
};
</script>

<style lang="less" scoped>
#timeline {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    height: 100%;
    margin: 0 10px;
    cursor: pointer;
    overflow: hidden;
}

#timeline .scrubber {
    position: absolute;
    top: 0;
    left: 0;
    width: 2px;
    height: 100%;
    background-color: #409eff;
    transition: left 0.2s linear;
}

#timeline .tick {
    position: absolute;
    top: 6px;
    width: auto;
    height: 15px;
    font-size: 12px;
}

#timeline .tick::after {
    content: "|";
    position: relative;
    top: -10px;
    left: calc(-100% + 2px);
    width: 1px;
    height: 4px;
    font-size: 12px;
}
</style>
